#!/bin/env/python3
from flask import Blueprint, request, session, redirect

admin_blue = Blueprint("admin", __name__, url_prefix='/admin')

from . import views


# 使用请求钩子 拦截用户请求  只有访问了admin_blue装饰的视图才会被拦截
# 1 拦截普通用户
# 2 拦截访问非登录页面
@admin_blue.before_request
def before_request():
    # if request.url.endswith("/admin/login"):
    #     pass
    # else:
    #     # 判断是否是管理员
    #     if not session.get("is_admin"):
    #         return redirect("/")

    # 代码改进：
    if not request.url.endswith("/admin/login"):
        if not session.get("is_admin"):
            return redirect('/')
